package com.singfee.gisservice.controller;

import com.singfee.gisservice.entity.AjaxResult;
import com.singfee.gisservice.entity.Ewkt;
import com.singfee.gisservice.entity.MyList;
import com.singfee.gisservice.service.IPostGISServer;
import io.micrometer.common.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/pg")
public class PostGISController {
    @Autowired
    IPostGISServer pgserver;

    @GetMapping("/test")
    public AjaxResult test(String token)
    {
        return AjaxResult.success("<<<<<<连接成功>>>>>>>");
    }

    @GetMapping("/features")
    public AjaxResult getFeatureList(@RequestParam String layerName,@RequestParam String filter) {
        try {
            String features=pgserver.getFeatureList(layerName, filter);
            return AjaxResult.success("请求成功",features);
        } catch (Exception ex) {
            return AjaxResult.error(ex.getMessage());
        }
    }

    @GetMapping("/featureInfo")
    public AjaxResult getFeatureInfo(String layerName,String filter) {
        try {
            return AjaxResult.success("请求成功", pgserver.getFeatureInfo(layerName, filter));
        } catch (Exception ex) {
            return AjaxResult.error(ex.getMessage());
        }
    }

    @GetMapping("/identify")
    public AjaxResult identify(@RequestParam String[] layerNames, @RequestParam String wkt,Integer srid) {
        try {
            if (layerNames.length>0&&StringUtils.isNotEmpty(wkt)) {
                Ewkt ewkt = Ewkt.builder().WKT(wkt).SRID(srid).build();
                HashMap<String,String> result = pgserver.identify(layerNames,ewkt,"");
                return AjaxResult.success("请求成功", result);
            } else {
                return AjaxResult.warn("请至少给出一个图层名称");
            }
        } catch (Exception ex) {
            return AjaxResult.error(ex.getMessage());
        }
    }

    @GetMapping("/identify2")
    public AjaxResult identify(@RequestParam String layerName, @RequestParam String wkt,Integer srid,@RequestParam String filterField) {
        try {
            if (StringUtils.isNotEmpty(layerName)&&StringUtils.isNotEmpty(wkt)) {
                Ewkt ewkt = Ewkt.builder().WKT(wkt).SRID(srid).build();
                String[] layerNames =new String[1];
                layerNames[0]=layerName;
                HashMap<String,String> result = pgserver.identify(layerNames,ewkt,filterField);
                return AjaxResult.success("请求成功", result);
            } else {
                return AjaxResult.warn("请至少给出一个图层名称");
            }
        } catch (Exception ex) {
            return AjaxResult.error(ex.getMessage());
        }
    }

    @GetMapping("/selectByGeom")
    public AjaxResult selectFeatureByGeom(@RequestParam String layerName,@RequestParam String WKT,Integer srid)
    {
        if(srid == null)
        {
            srid=4326;
        }
        Ewkt ewkt = Ewkt.builder().WKT(WKT).SRID(srid).build();
        MyList properties = new MyList().add("id").add("name");
        String result = pgserver.selectByPolygon(layerName,ewkt, properties);
        return  AjaxResult.success("查询成功！",result);
    }

    @GetMapping("/areaTotal")
    public AjaxResult TotalArea(@RequestParam String layerName,@RequestParam String wkt,Integer srid,@RequestParam String  filterFields) {
        try {
            if (srid == null) {
                srid = 4326;
            }
            if (StringUtils.isNotEmpty(layerName) && StringUtils.isNotEmpty(wkt)) {
                MyList property =new MyList();
                if(StringUtils.isNotEmpty(filterFields))
                {
                    String [] fields =filterFields.split(",");
                    for(String field:fields) {
                        property.add(field);
                    }
                }
//                System.out.println(wkt);
                Ewkt ewkt = Ewkt.builder().WKT(wkt).SRID(srid).build();
                List<Map> result = pgserver.TotalArea(layerName, ewkt,property);
                return AjaxResult.success("请求成功", result);
            } else {
                return AjaxResult.warn("图层名称或者wkt数据为空！", 0.0);
            }
        } catch (Exception ex) {
            return AjaxResult.error("请求出错了！", ex.getMessage());
        }
    }


    @GetMapping("/getGeomById")
    public AjaxResult getGeomById(String layerName,Integer id) {
        if(StringUtils.isNotEmpty(layerName)||id==null)
        {
            String geojson = pgserver.selectGeometryById(layerName, id);
            return AjaxResult.success("请求成功", geojson);
        }
        else {
            return AjaxResult.warn("参数" + layerName + ";" + id + "不能为空！");
        }
    }

    // 多边形选择（select）
    @GetMapping("/selectByPolygon")
    public AjaxResult selectByPolygon(String layerName,String wkt, Integer srid,String filterFields) {
        try {
            Ewkt ewkt = Ewkt.builder().WKT(wkt).SRID(srid).build();
            MyList properties = new MyList();
            if(StringUtils.isNotEmpty(filterFields))
            {
                String [] fields =filterFields.split(",");
                for(String field:fields) {
                    properties.add(field);
                }
            }
            String geojson = pgserver.selectByPolygon(layerName, ewkt, properties);
            return AjaxResult.success("请求成功", geojson);
        }catch (Exception ex)
        {
            return AjaxResult.error("请求成功，但发生了异常！",ex.getMessage());
        }
    }

    // 圈选（clip）
    @GetMapping("/clipByPolygon")
    public AjaxResult clipByPolygon( String layer,String wkt, Integer srid) {
        Ewkt ewkt = Ewkt.builder().WKT(wkt).SRID(srid).build();
        MyList properties = new MyList().add("id").add("name");
        String  geojson = pgserver.clipByPolygonWithProperties(layer,properties,ewkt);
        return AjaxResult.success("请求成功",geojson);
    }

    @PostMapping("/addFeature")
    public AjaxResult insert(String layerName,String wkt,Integer srid)
    {
        Ewkt ewkt = Ewkt.builder().WKT(wkt).SRID(srid).build();
        boolean isAdd= pgserver.addFeature(layerName,ewkt);
        return AjaxResult.success("数据添加成功！");
    }
}
